XfMissionChapterExecute={
	private ["_chapter_succeeded","_money_found","_traitor_killed","_last_random_reinforcement","_random_reinf_diff","_reinforcement_power"];
	diag_log "";
	diag_log "=========================================================================";
	diag_log "                             CHAPTER 1                                   ";
	diag_log "=========================================================================";
	diag_log "";
	diag_log Format["(%1)[%2]: %3; %4",diag_tickTime,"chapter1.sqf","XfMissionChapterExecute","Starting Chapter"];
	DM_Mission_Chapter_CanRun = True;
	DM_Mission_Chapter_Runs = True;
	DM_Mission_Chapter_Success = 0;
	call XfMissionChapterSetup;
	_chapter_succeeded = false;
	if (!DM_Mission_Mission_CanRun) exitWith {diag_log Format["(%1)[%2]: %3; %4",diag_tickTime,"chapter1.sqf","XfMissionChapterSetup","Mission canceled!"];};
	_money_task_updated = false;
	_kill_task_updated = false;
	_reinforcement_called = false;
	// We use a greater area to take into account wandering troops or reinforcement nearly there.
	_start_enemy_counts = ([DM_Chapter_Position,500,d_enemy_side] call XfMissionGetZoneSideCount) select 0;

	while {DM_Mission_Chapter_CanRun AND DM_Mission_Mission_CanRun} do {
		sleep 10;
		_enemy_counts = ([DM_Chapter_Position,500,d_enemy_side] call XfMissionGetZoneSideCount) select 0;
		_money_found = ["jip_money_found",false] call XNetGetJIP;
		_player_is_detected = ["jip_player_detected",false] call XNetGetJIP;
		if (!isNil "_money_found") then {
			if (_money_found and !_money_task_updated) then {
				["FindTheMoney","", "","",[],"Succeeded"] call XfMissionTask;
				_money_task_updated = true;
			};
		} else {
			_money_found = false;
		};
		_traitor_killed = ["jip_traitor_killed",false] call XNetGetJIP;
		if (!isNil "_traitor_killed") then {
			if (_traitor_killed and !_kill_task_updated) then {
				["KillTheTraitor","", "","",[],"Succeeded"] call XfMissionTask;
				_kill_task_updated = true;
			};
		} else {
			_traitor_killed = false;
		};
		if (_money_found AND _traitor_killed) then {
			_chapter_succeeded = true;
			DM_Mission_Chapter_CanRun = false;
		};
		If (DM_Mission_Chapter_CanRun) then {
			// This second IF will send a reinforcement as soon as the player is detected. No matter if more reinforcement will come later or not.
			// Sending random reinforcement if configured in the mission editor. But only after the first one took place.
			If (_reinforcement_called and _player_is_detected and (DM_Mission_RandomReinforcement > 0)) then {
				[d_enemy_side,[DM_Chapter_Position,100],DM_Mission_RandomReinforcement] call XfMissionRandomReinforcement;
				diag_log Format["(%1)[%2]: %3; %4",diag_tickTime,"chapter1.sqf","XfMissionChapterExecute","Random reinforcement sent!"];
				hint "Reinforcement";
			};
			// This is part of the chapter. The previous reinforcements are configurable.
			if ((!_reinforcement_called) AND (_player_is_detected) and (_enemy_counts  < (_start_enemy_counts / 2))) then {
				"You are detected! The enemy will send reinforcement!" call XfMissionMessageToAll;
				[d_enemy_side,[DM_Chapter_Position,100],1,"PARA",True] call XfMissionRandomReinforcement;
				_reinforcement_called = true;
			};
		};
	};
	if (DM_Mission_Mission_CanRun) then {
		if (_chapter_succeeded) then {
			call XfMissionChapterSuccess;
			sleep 15;
			["Ending Chapter 1",date] call XfMissionStartScreen;
			sleep 15;
		} else {
			call XfMissionChapterFailure;
		};
	} else {
		diag_log Format["(%1)[%2]: %3; %4",diag_tickTime,"chapter1.sqf","XfMissionChapterSetup","Mission canceled!"];
	};
	call XfMissionChapterCleanUp;
	DM_Mission_Chapter_Runs = False;
};

XfMissionChapterSetup={
	private ["_town_msta","_town_msta_pos","_town_msta_size","_grp","_grpSpecOps1","_grpSpecOps2","_grpBmp1","_grpVodnik1"];
	diag_log Format["(%1)[%2]: %3; %4",diag_tickTime,"chapter1.sqf","XfMissionChapterSetup","Starting Initialization"];
	_town_msta = d_town_markers select 21;
	_town_msta_pos = _town_msta select 0;
	_town_msta_size = _town_msta select 2;
	DM_Chapter_Position = _town_msta_pos;
	// This indicates where the mission takes place
	[_town_msta_pos,_town_msta_size,_town_msta select 1] call XfMissionCurrentPosition;
	// We move the mission start teleporter where the mission begins for vehicles, a bit north west of Msta behind the hill.
	[[10495.9,5812.55,0],88] call XfMissionPlaceStartTeleporter;

	_building_pos_hiding = [_town_msta_pos,_town_msta_size,"HIDE"] call XfMissionBuildingAreaPos;

	// We create the traitor
	["jip_traitor_killed",false] call XNetSetJIP;
	_grp = [d_enemy_side] call x_creategroup;
	_pos_traitor = [11327.9,5504.16,0.9];
	// Since the array is randomized, it will be different each time.
	if (count _building_pos_hiding > 0) then {_pos_traitor = _building_pos_hiding select 0};
	["RU_Profiteer2",_grp,_pos_traitor,"ASSASSINATE",["You killed the traitor","jip_traitor_killed"],"doStop this;"] call XfMissionCreateMissionUnit;
	
	// We create the money
	["jip_money_found",false] call XNetSetJIP;
	_pos_money = [11327.9,5503.16,0.9];
	// Since the array is randomized, it will be different each time.
	if (count _building_pos_hiding > 1) then {_pos_money = _building_pos_hiding select 1};
	["EvMoney",_pos_money,"PICKUP",["Olaf's Money","You found the money","jip_money_found","Take the money",True]] call XfMissionCreateMissionObject;

	// We create the tasks
	["FindTheMoney","Find Olaf's Money", "You need to find Olaf's money in the village of Msta before the russian special forces!","",[],"Created"] call XfMissionTask;
	["KillTheTraitor","Kill the traitor", "Find and kill the traitor who sold Olaf to the russians! There's a big chance he'll have the money with him. You can find him in the village of Msta!","",[],"Created"] call XfMissionTask;
	// We create the Mission markers on the map.
	[false,["chapter1_start_misson", [10495.9,5812.55,0], "ICON", "ColorBlue", [1,1],"Start",88,"hd_start","",1]] call XfMissionMarker;
	[false,["chapter1_msta_position", _town_msta_pos, "ICON", "ColorRed", [1,1],"Traitor",0,"hd_objective","",1]] call XfMissionMarker;
	// [d_enemy_side,[_town_msta_pos,_town_msta_size],true] call XfMissionPopulateLocation;

	// The trigger which will send reinforcement when the player is detected!
	["jip_player_detected",false] call XNetSetJIP;
	[_town_msta_pos,_town_msta_size * 1.5,d_own_side,d_enemy_side,"jip_player_detected"] call XfMissionSideDetected;
	
	[true,["upsmon_msta", _town_msta_pos, "ELLIPSE", "ColorBlue", [_town_msta_size,_town_msta_size], "Msta",0,"","",1]] call XfMissionMarker;
	// Spawns a group of specops in MSTA with the village as operating zone. They'll search buildings for enemies.
	_grpSpecOps1 = [d_enemy_side,[_town_msta_pos,_town_msta_size],["upsmon_msta","noveh","aware","buildings"],1] call XfMissionCreateFootmenGroup;
	// _grpSpecOps1 = [d_enemy_side,[_town_msta_pos,_town_msta_size],[],1] call XfMissionCreateFootmenGroup;
	// Spawns a group of specops in MSTA with the village as operating zone. They'll search buildings for enemies.
	// _grpSpecOps2 = [d_enemy_side,[_town_msta_pos,_town_msta_size],["upsmon_msta","noveh","aware","buildings"],1] call XfMissionCreateFootmenGroup;
	// We use the TaskDefend here to defend the area around the traitor's house.
	_grpSpecOps2 = [d_enemy_side,[_pos_traitor,15],[],1,false] call XfMissionCreateFootmenGroup;
	// Spawns a bmp type and its crew in MSTA with the village as operating zone.
	// _grpBmp1 = [d_enemy_side,[_town_msta_pos,_town_msta_size],["upsmon_msta","onroad","aware"],1] call XfMissionCreateVehicleGroup;
	_grpBmp1 = [d_enemy_side,[_town_msta_pos,_town_msta_size * 2],[],1] call XfMissionCreateVehicleGroup;
	// Spawns a vodnik type and its crew in MSTA with the village as operating zone.
	// _grpVodnik1 = [d_enemy_side,[_town_msta_pos,_town_msta_size],["upsmon_msta","onroad","aware"],2] call XfMissionCreateVehicleGroup;
	_grpVodnik1 = [d_enemy_side,[_town_msta_pos,_town_msta_size * 2],[],2] call XfMissionCreateVehicleGroup;
	
	// This will create snipers automatically if selected in the mission setup
	[d_enemy_side,[_town_msta_pos,_town_msta_size],DM_Mission_Snipers] call XfMissionCreateSnipersArea;
	// This will create air support automatically if selected in the mission setup
	[d_enemy_side,[_town_msta_pos,_town_msta_size],DM_Mission_AirSupport] call XfMissionCallAirSupport;
};

XfMissionChapterSuccess={
	diag_log Format["(%1)[%2]: %3; %4",diag_tickTime,"chapter1.sqf","XfMissionChapterSuccess","Starting"];
	DM_Mission_Chapter_Success = 1;
};

XfMissionChapterFailure={
	diag_log Format["(%1)[%2]: %3; %4",diag_tickTime,"chapter1.sqf","XfMissionChapterFailure","Starting"];
	DM_Mission_Chapter_Success = -1;
};

// Self-explanatory
XfMissionChapterCleanUp={
	diag_log Format["(%1)[%2]: %3; %4",diag_tickTime,"chapter1.sqf","XfMissionChapterCleanUp","Starting"];
	// Giving an empty position places the teleporter back to the hangar, closing the hangar.
	[[],0] call XfMissionPlaceStartTeleporter;
	// Giving only the marker name will trigger the deletion of the marker.
	[false,["chapter1_start_misson"]] call XfMissionMarker;
	[false,["chapter1_msta_position"]] call XfMissionMarker;
	[true,["upsmon_msta"]] call XfMissionMarker;
	[] call XfMissionCleanObjects;
	[] call XfMissionCleanTriggers;
	[] call XfMissionCleanTasks;
};

DM_Mission_Chapter_Compiled = true;